<?php
namespace Todo\Model\Sys;

/**
 * Class OrganizationModel 客户机构 模型
 * @package Todo\Model\Sys
 */
class OrganizationModel extends SysBaseModel
{
    protected $_validate = array(
        array("title", "require", "机构名称必须！"),
        array("title", "1,50", "机构名称长度超出限制！", 3, "length"),
        array("trade_id", "require", "所在行业必须！"),
        array("phone", "require", "联系电话必须！"),
        array("phone", "/^1[34578]\d{9}$/", "联系电话必须为手机号！", 3, "regex"),
        array("linkman", "require", "联系人姓名必须！"),
        array("linkman", "1,20", "联系人姓名长度超出限制！", 3, "length"),
        array("email", "require", "邮箱必须！"),
        array("email", "email", "邮箱必须为邮箱地址！"),
        array("provice", "require", "省份必须！"),
        array("city", "require", "地市必须！"),
        array("district", "require", "区县必须！"),
        array("address", "require", "详细地址必须！"),
        array("address", "1,200", "详细地址长度超出限制！", 3, "length"),
        array("postcode", "require", "邮编必须！"),
        array("postcode", "6", "邮编长度超出限制！", 3, "length"),
        array("status", "require", "状态必须！")
    );

    protected $_auto = array(
        array("is_del", "0"),
        array("add_time", 'time', 1, 'function')
    );

    public function format(&$info, $fields = array('status'))
    {
        if(!$fields) return;

        if(!is_array($fields)) $fields = explode(',', $fields);
        foreach($fields as $field){
            if($field == 'trade' && $info['trade_id']){
                $info['trade_name'] = D('Todo/Sys/Trade')->keyFind($info['trade_id'], 'title');
            }
        }
    }
}